#! /bin/bash
#    salt批量执行report.sh脚本并获取数据：salt '*' cmd.run '/home/t/report.sh'
#	过滤shell颜色字符：sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
#	salt '*' cmd.run '/home/t/report.sh'|sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" > /home/t/report
export PATH

SAR_PATH='/var/log/sysstat'
if [ ! -d $SAR_PATH ] ;
then
	SAR_PATH='/var/log/sa'
fi
SA_FILE=`ls $SAR_PATH |egrep 'sa[0-9][0-9]'`
rm -f /tmp/cpu_5_12 /tmp/mem_5_12 /tmp/cpu_12_5 /tmp/mem_12_5 /tmp/sysinfo

for sa in $SA_FILE;
do
    (sar -u -s 05:30:00 -f $SAR_PATH/$sa |egrep 'all'|egrep -v 'Average' >> /tmp/cpu_5_12) 2> /dev/null
    (sar -r -s 05:30:00 -f $SAR_PATH/$sa |egrep -v 'LINUX|Linux|kbmemfree|Average' >> /tmp/mem_5_12) 2> /dev/null

    (sar -u -s 00:30:00 -e 05:30:00 -f $SAR_PATH/$sa |egrep 'all'|egrep -v 'Average' >> /tmp/cpu_12_5) 2> /dev/null
    (sar -r -s 00:30:00 -e 05:30:00 -f $SAR_PATH/$sa |egrep -v 'LINUX|Linux|kbmemfree|Average' >> /tmp/mem_12_5) 2> /dev/null

done

HOST=$(hostname -s)
IP=$(hostname -I)
CPU_NUM=$(cat /proc/cpuinfo |grep processor|wc -l)
MEM_TOTAL=$(cat /proc/meminfo|grep MemTotal|awk -F'[: ]+' '{print $2}')

echo 'HOST:'$HOST >> /tmp/sysinfo
echo 'IP:'$IP >> /tmp/sysinfo
echo 'CPU_NUM:'$CPU_NUM >> /tmp/sysinfo
echo 'MEM_TOTAL:'$MEM_TOTAL >> /tmp/sysinfo

(/usr/bin/python /tmp/sar_report.py) 2> /dev/null